package org.flyby.api.daoImpl;

import com.google.common.collect.Maps;
import org.flyby.api.dao.CartDao;
import org.flyby.api.persistence.Cart;
import org.flyby.api.persistence.ProductOrderDetails;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
public class CartDaoImpl extends BaseCommonDaoImpl<Cart> implements CartDao {


    @Override
    public void addProductOrderDetails(Cart cart, ProductOrderDetails productOrderDetails) {
        updateCartTotalPriceAndTotalProductsByProductOrderDetails(cart, productOrderDetails);
    }

    private void updateCartTotalPriceAndTotalProductsByProductOrderDetails(Cart cart, ProductOrderDetails productOrderDetails) {
        Map<String, Object> parameters = Maps.newHashMap();
        parameters.put("cartId", cart.getId());
        parameters.put("orderPrice", productOrderDetails.getOrderPrice());
        parameters.put("orderWeight", productOrderDetails.getOrderWeight());
        parameters.put("units", productOrderDetails.getUnits());

        executeUpdateNamedQuery("Cart.updateCartTotalPriceAndTotalProductsByProductOrderDetails", parameters);
    }
}
